package com.xj0927.util;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-11-14 15:25
 * @Modified By:
 */
@Component
public class FileDfsUtil {

    @Resource
    private FastFileStorageClient storageClient;

    /**
     * @Description: 上传文件
     * @Param:
     * @Return:
     * @Author: xj0927
     * @Date Created in 2020/11/14 15:26
     */
    public String upload(MultipartFile multipartFile) throws IOException {
        String filename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), filename, null);
       return storePath.getFullPath();
    }

    /**
    * @Description: 删除文件
    * @Param:
    * @Return:
    * @Author: xj0927
    * @Date Created in 2020/11/14 15:29
    */
    public void deleteFile(String fileUrl){
        StorePath storePath = StorePath.parseFromUrl(fileUrl);
        storageClient.deleteFile(storePath.getGroup(),storePath.getPath());
    }

}
